package com.hong610.service.impl;

import com.hong610.quartz.QuartzStatus;
import com.hong610.quartz.core.QuartzCore;
import com.hong610.quartz.core.QuartzManager;
import com.hong610.quartz.entity.JobClassInfo;
import com.hong610.quartz.entity.JobInfo;
import com.hong610.quartz.extend.QuartzJob;
import com.hong610.service.QuartzService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Hong on 2017/2/9.
 */
@Service("QuartzService")
public class QuartzServiceImpl implements QuartzService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    public List<JobClassInfo> getQuartzJobClass() throws Exception {
        return QuartzJob.getQuartzJobClass();
    }

    public List<JobInfo> getJobAll() {
        List<JobInfo> list = new ArrayList();
        List<Map<String, Object>> dataResult = jdbcTemplate.queryForList("select * from qrtz_triggers");
        for (Map<String, Object> row : dataResult) {
            JobInfo jobInfo = new JobInfo();
            jobInfo.setSchedName(row.get("SCHED_NAME") == null ? "" : row.get("SCHED_NAME").toString());
            jobInfo.setTriggerName(row.get("TRIGGER_NAME") == null ? "" : row.get("TRIGGER_NAME").toString());
            jobInfo.setTriggerGroup(row.get("TRIGGER_GROUP") == null ? "" : row.get("TRIGGER_GROUP").toString());
            jobInfo.setJobName(row.get("JOB_NAME") == null ? "" : row.get("JOB_NAME").toString());
            jobInfo.setJobGroup(row.get("JOB_GROUP") == null ? "" : row.get("JOB_GROUP").toString());
            jobInfo.setDescription(row.get("DESCRIPTION") == null ? "" : row.get("DESCRIPTION").toString());
            jobInfo.setNextFireTime(row.get("NEXT_FIRE_TIME") == null ? 0L : (Long) row.get("NEXT_FIRE_TIME"));
            jobInfo.setPrevFireTime(row.get("PREV_FIRE_TIME") == null ? 0L : (Long) row.get("PREV_FIRE_TIME"));
            jobInfo.setPriority(row.get("PRIORITY") == null ? 0 : (Integer) row.get("PRIORITY"));
            jobInfo.setTriggerState(row.get("TRIGGER_STATE") == null ? "" : row.get("TRIGGER_STATE").toString());
            jobInfo.setTriggerState(QuartzStatus.getStatus(jobInfo.getTriggerState()));
            jobInfo.setTriggerType(row.get("TRIGGER_TYPE") == null ? "" : row.get("TRIGGER_TYPE").toString());
            jobInfo.setStartTime(row.get("START_TIME") == null ? 0 : (Long) row.get("START_TIME"));
            jobInfo.setCronExpression(QuartzJob.getCronExpression(jobInfo.getTriggerName(), jobInfo.getTriggerGroup()));
            jobInfo.setJobClass(QuartzJob.getJobClass(jobInfo.getTriggerName(), jobInfo.getTriggerGroup()));
            list.add(jobInfo);
        }
        return list;
    }

    @Override
    public boolean restart(String triggerName, String triggerGroup) {
        try {
            QuartzManager.instance().restart(triggerName, triggerGroup);
            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean pause(String triggerName, String triggerGroup) {
        try {
            QuartzManager.instance().pause(triggerName, triggerGroup);
            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean remove(String triggerName, String triggerGroup) {
        try {
            QuartzManager.instance().remove(triggerName, triggerGroup);
            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public JobInfo findOne(String triggerName, String triggerGroup) {
        List<Map<String, Object>> dataResult = jdbcTemplate.queryForList("select * from qrtz_triggers where TRIGGER_NAME=? and TRIGGER_GROUP=?", triggerName, triggerGroup);
        for (Map<String, Object> row : dataResult) {
            JobInfo jobInfo = new JobInfo();
            jobInfo.setSchedName(row.get("SCHED_NAME") == null ? "" : row.get("SCHED_NAME").toString());
            jobInfo.setTriggerName(row.get("TRIGGER_NAME") == null ? "" : row.get("TRIGGER_NAME").toString());
            jobInfo.setTriggerGroup(row.get("TRIGGER_GROUP") == null ? "" : row.get("TRIGGER_GROUP").toString());
            jobInfo.setJobName(row.get("JOB_NAME") == null ? "" : row.get("JOB_NAME").toString());
            jobInfo.setJobGroup(row.get("JOB_GROUP") == null ? "" : row.get("JOB_GROUP").toString());
            jobInfo.setDescription(row.get("DESCRIPTION") == null ? "" : row.get("DESCRIPTION").toString());
            jobInfo.setNextFireTime(row.get("NEXT_FIRE_TIME") == null ? 0L : (Long) row.get("NEXT_FIRE_TIME"));
            jobInfo.setPrevFireTime(row.get("PREV_FIRE_TIME") == null ? 0L : (Long) row.get("PREV_FIRE_TIME"));
            jobInfo.setPriority(row.get("PRIORITY") == null ? 0 : (Integer) row.get("PRIORITY"));
            jobInfo.setTriggerState(row.get("TRIGGER_STATE") == null ? "" : row.get("TRIGGER_STATE").toString());
            jobInfo.setTriggerState(QuartzStatus.getStatus(jobInfo.getTriggerState()));
            jobInfo.setTriggerType(row.get("TRIGGER_TYPE") == null ? "" : row.get("TRIGGER_TYPE").toString());
            jobInfo.setStartTime(row.get("START_TIME") == null ? 0 : (Long) row.get("START_TIME"));
            jobInfo.setCronExpression(QuartzJob.getCronExpression(jobInfo.getTriggerName(), jobInfo.getTriggerGroup()));
            jobInfo.setJobClass(QuartzJob.getJobClass(jobInfo.getTriggerName(), jobInfo.getTriggerGroup()));
            return jobInfo;
        }
        return null;
    }

    @Override
    public boolean edit(JobInfo jobInfo) {
        try {
            QuartzManager.instance().edit(jobInfo.getTriggerName(), jobInfo.getTriggerGroup(), jobInfo.getCronExpression());
            return true;
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}
